﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Models;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI.Controllers {

    public class ProductController : Controller {
        public int PageSize = 4;// we will change this later
        private IProductRepository repository;

        public ProductController(IProductRepository repository) {
            this.repository = repository;
        }

        //public ActionResult List() {
        //    return View(repository.Products);
        //}

        public ActionResult List(string category, int page = 1) {
            ProductsListViewModel viewModel = new ProductsListViewModel {
                Products = repository.Products
                                     .Where(p => category == null || p.Category == category)
                                     .OrderBy(p => p.ProductID)
                                     .Skip((page - 1) * PageSize)
                                     .Take(PageSize),
                PagingInfo = new PagingInfo {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = category == null 
                                    ? repository.Products.Count() 
                                    : repository.Products.Where(x => x.Category == category).Count()
                },
                CurrentCategory = category
            };

            return View(viewModel);
        }

        public FileContentResult GetImage(int productId) {
            Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (product != null) {
                return File(product.ImageData, product.ImageMimeType);
            }
            return null;
        }
    }
}
